package com.yuxiang.flowable.util;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author : yx-0176
 * @description
 * @date : 2022/3/9
 */
@Slf4j
public class OssUtil {

    private static final String BASE_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

//    public static void main(String[] args) {
//        String fileName = "asdfsadfasdfsadf哈哈哈.pdf";
//        Integer index = fileName.lastIndexOf(".");
//        String fileExtension = index != -1 ? fileName.substring(fileName.lastIndexOf(".")) : "";
//        fileName = index != -1 ? fileName.substring(0, index) : fileName;
//        fileName = fileName + RandomUtil.randomString(BASE_STR, 5) + fileExtension;
//        System.out.println(fileName);
//    }

    /**
     * 上传文件
     *
     * @param inputStream
     * @param fileName
     * @param packageName
     * @param bucketName
     * @return
     */
    public static synchronized String uploadFile(InputStream inputStream, String fileName, String packageName, String bucketName) {

        try {
            byte[] bytes = getBytes(inputStream);

            // 文件名称重命名
//            String degist = getDegist(bytes);
//            for (int i = degist.length(); i < 32; i++) {
//                degist = "0" + degist;
//            }

            Integer index = fileName.lastIndexOf(".");
            String fileExtension = index != -1 ? fileName.substring(fileName.lastIndexOf(".")) : "";
            fileName = index != -1 ? fileName.substring(0, index) : fileName;
            fileName = fileName + RandomUtil.randomString(BASE_STR, 5) + fileExtension;

            Long fileSize = 0L + bytes.length;

            String diskName = packageName + "/";
            String OSS = Oss.uploadObject2OSS(new ByteArrayInputStream(bytes), fileName, fileSize, bucketName, diskName);

//            if (fileName.equalsIgnoreCase(OSS + fileExtension)) {
//                return diskName + fileName;
//            } else {
//                throw new RuntimeException("Fail to upload file ,");
//            }
            return diskName + fileName;

        } catch (Exception e) {
            throw new RuntimeException("Fail to upload file ,");
        }

    }


    /**
     * 加密
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String getDegist(byte[] bytes) throws Exception {

        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(bytes);

        String md5 = new BigInteger(1, messageDigest.digest()).toString(16);
        return md5;

    }


    public static byte[] getBytes(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
        byte[] b = new byte[4096];
        int n;
        while ((n = in.read(b)) != -1) {
            out.write(b, 0, n);
        }
        byte[] ret = out.toByteArray();
        in.close();
        out.close();
        return ret;
    }

}
